<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar @input="input" cancelButton="none" radius="20" focus="teue"></uni-search-bar>
		</view>
		
		<!-- 搜索列表 -->
		<view class="search-results-list" v-if="searchResults.length > 0">
			<view class="search-results-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
				<view class="search-results-item-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-container" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="18"></uni-icons>
			</view>
			<view class="history-keywords">
				<uni-tag :text="item" v-for="(item,i) in historyKeywords" :key="i"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖 延时timeid
				timerId: null,
				// 搜索关键字
				kw: '',
				// 搜索列表数据
				searchResults: [],
				// 搜索历史列表
				historyKeywords: []
			};
		},
		onLoad() {
			this.historyKeywords = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(value) {
				// 清
				clearTimeout(this.timerId)
				// 重建
				this.timerId = setTimeout(() => {
					// console.log('重新计时', this.timerId)
					console.log(value)
					this.kw = value
					this.getSearchResults()
					if(!this.kw.trim()) {
						this.searchResults = []
					}
				}, 1000)
			},
			// 获取搜索列表
			async getSearchResults() {
				const {data: res} = await uni.$http.get('/goods/qsearch', {query: this.kw})
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				// 搜索历史
				this.saveSearchHistory()
			},
			// 点击 数据 跳转 详情页
			gotoDetail(goods_id) {
				uni.navigateTo({
					 url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			// 搜索历史
			saveSearchHistory() {
				// this.historyKeywords.unshift(this.kw)
				// 查找相同历史的索引
				const index = this.historyKeywords.indexOf(this.kw)
				if(index<0) {
					// 无相同  添加 首位
					this.historyKeywords.unshift(this.kw)
				} else {
					// 有相同  删除 首位添加
					this.historyKeywords.splice(index,1)
					this.historyKeywords.unshift(this.kw)
				}
				// 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyKeywords))
			}
		}
	}
</script>

<style lang="scss">
.search-box {
  background-color: #c00000;
  position: sticky;
  top: 0;
  z-index: 9;
}
.search-results-list {
  padding: 0 20rpx;
  .search-results-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1rpx solid #efefef;
    height: 90rpx;
    .search-results-item-name {
      font-size: 24rpx;
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
.history-container {
	.history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		padding: 0 20rpx;
		border-bottom: 1rpx solid #efefef;
	}
	.history-keywords {
		display: flex;
		flex-wrap: wrap;
		uni-tag {
			margin: 10rpx;
		}
	}
}
</style>
